package team.sun.sss.controller.hardwork;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import team.sun.sss.dao.hardwork.HwStatisticsDao;
import team.sun.sss.pojo.hardwork.HwApply;
import team.sun.sss.pojo.hardwork.HwStatistics;

import java.util.List;


@Controller
public class HwStateController {
    @Autowired
    HwStatisticsDao hwStatisticsDao;

    @RequestMapping("/hw_staff_state")
    public String staffState(Model model ,@RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
                             @RequestParam(defaultValue="5",value="pageSize")Integer pageSize){
        if(pageNum == null){
            pageNum = 1;   //设置默认当前页
        }
        if(pageNum <= 0){
            pageNum = 1;
        }
        if(pageSize == null){
            pageSize = 5;    //设置默认每页显示的数据数
        }
        //1.引入分页插件,pageNum是第几页，pageSize是每页显示多少条,默认查询总数count
        PageHelper.startPage(pageNum,pageSize);
        //2.紧跟的查询就是一个分页查询-必须紧跟.后面的其他查询不会被分页，除非再次调用PageHelper.startPage
        try {
            List<HwStatistics> studentList = hwStatisticsDao.getHwStatisticsList();
            model.addAttribute("HwStateList",studentList);
            //3.使用PageInfo包装查询后的结果,5是连续显示的条数,结果list类型是Page<E>
            System.out.println("分页数据："+studentList);
            PageInfo<HwStatistics> pageInfo = new PageInfo<HwStatistics>(studentList,pageSize);
            //4.使用model/map/modelandview等带回前端
            model.addAttribute("pageInfo",pageInfo);
        }finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }
        return "hardWork/hw_staff_state";

    }
}
